<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>文档</title>
    <link href="../css/semantic/semantic.min.css" rel="stylesheet">
    <link href="../css/markdown.css" rel="stylesheet">
    <link href="../css/wtxynavigation.css" rel="stylesheet">
    <script src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/include.js"></script>
    <style>
        body {
            padding-top: 0;
            background: #f7f7f7
        }

        body > .ui.container {
            width: 100%;
            padding: 0
        }

        #project-title {
            background: #6e6e6e;
            width: 100%;
            line-height: 3.572rem;
            color: #fff;
            font-size: 1.2rem
        }

        #project-title .title-wrap {
            font-weight: 700;
            padding: 0 12px;
            width: 100%;
            max-width: 1366px !important;
            min-width: 900px;
            margin: 0 auto
        }

        #project-title .left, #project-title right {
            display: inline-block
        }

        #project-title .right {
            float: right
        }

        #project-title .right .wiki-actions .menu {
            left: auto;
            right: 0;
            margin-top: .4em !important
        }

        #wiki-preview-container {
            width: 100%;
            max-width: 1366px !important;
            min-width: 900px;
            padding: 0;
            border: 1px solid #e3e9ed;
            border-top: 0
        }

        #wiki-preview > .ui.segment {
            margin-top: 0;
            padding: 1rem;
            padding-left: 0;
            border: 0
        }

        .hover-places {
            display: inline-block
        }

        #page-detail {
            width: 60%;
            margin: 0 auto
        }

        #page-detail .title {
            font-weight: 700;
            font-size: 24px;
            padding: 12px 0;
            border-bottom: 1px solid #ccc
        }

        #page-detail .content {
            margin-top: 15px
        }

        .content p {
            word-wrap: break-word
        }

        #wiki-parent, #wiki-title, #wikis {
            display: none
        }

        .ui.grid {
            padding: 0 !important
        }

        #ex-all {
            cursor: pointer;
            color: #537c8d
        }
    </style>
</head>
<body>
<div class="ui container">

    <div id="wtxyNav"></div>

<!--    <nav id="wtxyNav" class="navbar navbar-inverse">-->

<!--        <div class="title-wrap">-->

<!--            <div class="left">-->
<!--                <a class="wtxyNavLeftTitle" href="../../../index.html">ITOpenGL</a>-->
<!--            </div>-->

<!--            <div class="right">-->
<!--                <a class="wtxyNavRightTitle" href="../../../index.html?type=1">iOS面试</a>-->
<!--                <a class="wtxyNavRightTitle" href="../../../index.html?type=2">音视频面试</a>-->
<!--                <a class="wtxyNavRightTitle" href="../../../index.html?type=3">java面试</a>-->
<!--            </div>-->
<!--        </div>-->
<!--    </nav>-->


    <div class="ui container" id="wiki-preview-container">
        <div id="wiki-preview">
            <div class="ui segment">
                <div id="page-detail" class="markdown-body">
                    <div class='title'>28.runtime方法交换</div>
                    <div class='content'><h1>
                        <a id="runtime的方法交换method-swizzling" class="anchor"
                           href="#runtime%E7%9A%84%E6%96%B9%E6%B3%95%E4%BA%A4%E6%8D%A2method-swizzling"></a>runtime的方法交换Method-Swizzling
                    </h1>
                        <p><strong>关键词</strong>: <code>Method-Swizzling</code>,</p>
                        <h1>
                            <a id="1-class_getinstancemethod" class="anchor" href="#1-class_getinstancemethod"></a>1.
                            class_getInstanceMethod</h1>
                        <pre><code>
class_getInstanceMethod(Class  _Nullable cls, SEL  _Nonnull name)

</code></pre>
                        <ul>
                            <li>第一个参数<code>Class cls</code>: 就是<code>Self</code>, 查看源码是类对象</li>
                            <li>第二个参数<code>SEL name</code> : 方法选择器名称</li>
                        </ul>
                        <h1>
                            <a id="2-class_getclassmethod" class="anchor" href="#2-class_getclassmethod"></a>2.
                            class_getClassMethod</h1>
                        <pre><code>
class_getClassMethod(Class  _Nullable cls, SEL  _Nonnull name)

</code></pre>
                        <ul>
                            <li>第一个参数<code>Class cls</code>: 就是<code>Self</code>, 查看源码是类对象</li>
                            <li>第二个参数<code>SEL name</code> : 方法选择器名称</li>
                        </ul>
                        <h1>
                            <a id="3-method_exchangeimplementations" class="anchor"
                               href="#3-method_exchangeimplementations"></a>3. method_exchangeImplementations</h1>
                        <pre><code>
method_exchangeImplementations(Method  _Nonnull m1, Method  _Nonnull m2)

</code></pre>
                        <ul>
                            <li>第一个参数<code>Method m1</code>: 原来方法</li>
                            <li>第二个参数<code>Method m2</code> : 替换方法</li>
                        </ul>
                        <h1>
                            <a id="示例代码" class="anchor" href="#%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81"></a>示例代码</h1>
                        <pre><code>


#import "WTUserTest.h"

#import &lt;objc/runtime.h&gt;

@implementation WTUserTest

+ (void)load
{
    Method test = class_getInstanceMethod(self, @selector(test));
    
    Method test2 = class_getInstanceMethod(self, @selector(test2));
    
    method_exchangeImplementations(test, test2);
    
    
    Method classtest = class_getClassMethod(self, @selector(test));
    Method classtest2 = class_getClassMethod(self, @selector(test2));
    method_exchangeImplementations(classtest, classtest2);
    
}


// 原来方法
+ (void)test
{
    NSLog(@"类-吃饭了没?........");
}

// 替换方法
+ (void)test2
{
   
    [self test2];
    NSLog(@"类-晚上好啊~........");
}



// 源方法
- (void)test
{
    NSLog(@"吃饭了没?........");
}

// 替换方法
- (void)test2
{
   
    [self test2];
    NSLog(@"晚上好啊~........");
}
@end

</code></pre>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>